package io.sxl.modules.generator.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.sxl.modules.generator.entity.HouseEntity;
import io.sxl.modules.generator.entity.ProjectEntity;
import io.sxl.modules.generator.entityx.DiaoBoEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.sxl.modules.generator.entity.FamilyEntity;
import io.sxl.modules.generator.service.FamilyService;
import io.sxl.common.utils.PageUtils;
import io.sxl.common.utils.R;


/**
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-03-26 20:53:01
 */
@RestController
@RequestMapping("generator/family")
@Slf4j
public class FamilyController {
    @Autowired
    private FamilyService familyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:family:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = null;
        String s = null;
        try {
            s = params.get("key").toString();
        } catch (NullPointerException e) {
            page = familyService.queryPage(params);
            log.info("key is null do queryPage");
        }
        if (s != null && !"".equals(s)) {
            page = familyService.queryById(params);
        }
        if ("".equals(s)) {
            page = familyService.queryPage(params);
        }


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("generator:family:info")
    public R info(@PathVariable("id") Integer id) {
        FamilyEntity family = familyService.getById(id);

        return R.ok().put("family", family);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:family:save")
    public R save(@RequestBody FamilyEntity family) {
        familyService.save(family);

        return R.ok();
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:family:update")
    public R update(@RequestBody FamilyEntity family) {
        familyService.updateById(family);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:family:delete")
    public R delete(@RequestBody Integer[] ids) {
        familyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据拆迁人的房屋关联查询
     */
    @RequestMapping("/gethousebyuser")
    //@RequiresPermissions("generator:family:delete")
    public R get(@RequestParam Map<String, Object> params){
        String key =(String) params.get("key");
        System.out.println("key = " + key);
        if (key.matches("^[0-9]*$")) {
            params.put("id",Integer.parseInt(key));


        }
        //匹配汉字
        if (key.matches("^[\\u4e00-\\u9fa5]*$")){
            params.put("name",""+params.get("key"));

        }
        PageUtils pageUtils = familyService.queryByUser(params);

        return R.ok().put("page",pageUtils);
    }





}
